package com.spring.gop.proj.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.spring.gop.proj.interfaces.MovieService;
import com.spring.gop.proj.model.Buy;
import com.spring.gop.proj.model.MovieAttach;
import com.spring.gop.proj.model.User;

@Controller
@RequestMapping("/movie/*")
@SessionAttributes("movie")
public class MovieController {

	@Autowired private MovieService movieService;
	
	@RequestMapping
	public void list(Model model, String searchMovieName) {
		model.addAttribute("pageIdx", 1);
		if(searchMovieName == null || searchMovieName.equals(""))model.addAttribute("movies", movieService.selectMovies());
		else model.addAttribute("movies", movieService.selectMovieForTitle(searchMovieName));
	}
	
	@RequestMapping(value="/moviedetail", method=RequestMethod.GET)
	public void moviedetail(Model model, int num, HttpSession session) {
		model.addAttribute("pageIdx", 2);
		session.setAttribute("movieNum", num);
		User user = (User)session.getAttribute("user");
		if(user != null && !user.getMember_id().equals("admin")) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("member_id", user.getMember_id());
			map.put("movie_num", num);
			
			Buy buy = movieService.selectBuyMovieForId(map);
			/*System.out.println(buy.getMemberId());
			System.out.println(buy.getMovieNum());*/
			model.addAttribute("userId", user.getMember_id());
			model.addAttribute("buy", buy);
			//model.addAttribute("buy", movieService.selectBuyMovieForId(map));
		}
		
		List<MovieAttach> list = movieService.selectMovieAttach(num);
		
		MovieAttach poster = null;
		List<MovieAttach> stillcut = new ArrayList<MovieAttach>();
		List<MovieAttach> thumb = new ArrayList<MovieAttach>();
		MovieAttach trailer = null;
		MovieAttach content = null;
		MovieAttach background = null;
		
		for(int i=0; i<list.size(); i++) {
			if(list.get(i).getAttach_type().equals("poster")) poster = list.get(i);
			else if(list.get(i).getAttach_type().equals("stillcut")) stillcut.add(list.get(i));
			else if(list.get(i).getAttach_type().equals("thumbnail")) thumb.add(list.get(i));
			else if(list.get(i).getAttach_type().equals("trailer")) trailer = list.get(i);
			else if(list.get(i).getAttach_type().equals("content")) content = list.get(i);
			else if(list.get(i).getAttach_type().equals("background")) background = list.get(i);
		}
		model.addAttribute("movie", movieService.selectMovie(num));
		model.addAttribute("poster", poster);
		model.addAttribute("stillcut", stillcut);
		model.addAttribute("thumb", thumb);
		model.addAttribute("trailer", trailer);
		model.addAttribute("content", content);
		model.addAttribute("background", background);
	}
	
	@RequestMapping(value="/buyMovie", method=RequestMethod.POST)
	public String buyMovie(@ModelAttribute("newBuy")Buy buy, HttpSession session) {
		
		User newUser = (User)session.getAttribute("user");
		movieService.insertBuyMovie(buy, newUser);
		session.setAttribute("user", newUser);
		
		return "redirect:" + buy.getReturn_url();
	}
}



















































